package cn.bingo.yunsj.utils;

import cn.bingo.yunsj.mail.model.ConfigProperties;
import cn.bingo.yunsj.model.HrUserSimpleInfo;
import com.google.gson.Gson;
import lombok.Data;
import okhttp3.*;

import javax.net.ssl.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Data
public class HttpClientUtils<T> {

    private String CompanyID;

    public HttpClientUtils() {
        ConfigProperties configProperties = new ConfigProperties();
        CompanyID = configProperties.getCompanyID();
    }

    public T SimpleInfo(String url,T model) throws IOException {
        OkHttpClient okHttpClient = getUnsafeOkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        Map<String, String> map = new HashMap<>();
        map.put("CompanyID", CompanyID);
        final Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(mediaType, new Gson().toJson(map)))
                .build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        String string = response.body().string();
        model = (T) new Gson().fromJson(string,model.getClass());
        new OkHttpClient();
        return model;
    }

    // 重写Okhttp，为了让它完全信任证书
    private OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }
                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }
                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };
            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory);
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
    }
    }
}
